import {
	get,
	post,
	put,
	del,
	baseUrl
} from '@/common/utils/request';

const petApi = {
	/**
	 * 根据用户ID查询到宠物主人id
	 */
	getPetOwnerIdByUserId: () => {
		return get(`/api/pet/ownerIdByUserId`);
	},
	/**
	 * 根据用户ID查询到宠物主人id再查询宠物列表
	 */
	getPetsByUserId: () => {
		return get(`/api/pet/user`);
	},
	/**
	 * 根据宠物ID查询宠物详情
	 */
	getPetById: (petId) => {
		return get(`/api/pet/getPetById/${petId}`);
	},
	/**
	 * OSS宠物头像
	 */
	uploadImage: (tempFilePath) => {
		const token = uni.getStorageSync('token');
		const retoken = uni.getStorageSync('retoken');
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: `${baseUrl}/api/pet/upload/image`,
				filePath: tempFilePath,
				name: 'file',
				header: {
					authorization: 'Bearer ' + token,
					retoken: 'Bearer ' + retoken
				},
				success: (res) => {
					try {
						const result = JSON.parse(res.data);
						if (result.code === 200 && result.data) {
							resolve(result.data);
						} else {
							reject(new Error(result.message || '图片上传失败'));
						}
					} catch (e) {
						reject(new Error('图片上传响应解析失败'));
					}
				},
				fail: (err) => {
					reject(new Error(`上传接口调用失败: ${err.errMsg}`));
				}
			});
		});
	},
	/**
	 * 添加宠物
	 */
	addPet: (pet) => {
		return post('/api/pet/addPet', pet);
	},
	updatePet: (pet) => {
		return post('/api/pet/updatePet', pet);
	}
};

export default petApi;